package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"trade-guard/cache"
	"trade-guard/models"
)

func CarAdd(c *gin.Context) {
	car_code := c.PostForm("car_code")
	car_type := c.PostForm("car_type")
	car_price, _ := strconv.Atoi(c.PostForm("car_price"))
	data := models.Car{
		CarCode:  car_code,
		CarType:  car_type,
		CarPrice: car_price,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func CarOrderAdd(c *gin.Context) {
	car_order_code := c.PostForm("car_order_code")
	car_order_name := c.PostForm("car_order_name")
	car_order_price, _ := strconv.Atoi(c.PostForm("car_order_price"))
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	data := models.CarOrder{
		CarOrderCode:  car_order_code,
		CarOrderName:  car_order_name,
		CarOrderPrice: car_order_price,
		Cid:           cid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func CarOrderList(c *gin.Context) {

	/*
		SELECT
		  car_orders.id,
		  car_orders.car_order_code,
		  car_orders.car_order_name,
		  car_orders.car_order_price,
		  cars.car_code,
		  cars.car_type,
		  cars.car_price
		FROM
		  car_orders
		  LEFT JOIN cars ON car_orders.cid = cars.id
	*/
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var list []models.CarOrderList
	redis_key := "CarOrderList_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("car_orders").
			Select(
				"car_orders.id",
				"car_orders.car_order_code",
				"car_orders.car_order_name",
				"car_orders.car_order_price",
				"cars.car_code",
				"cars.car_type",
				"cars.car_price",
			).
			Joins("LEFT JOIN cars ON car_orders.cid = cars.id").
			Offset(offset).Limit(size).Find(&list)

		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	var total int64
	models.DB.Model(&models.CarOrder{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"data":  list,
		"total": total,
		"msg":   "ok",
	})
	return

}
func CarOrderDetail(c *gin.Context) {

	/*
	   SELECT
	   		  car_orders.id,
	   		  car_orders.car_order_code,
	   		  car_orders.car_order_name,
	   		  car_orders.car_order_price,
	   		  cars.car_code,
	   		  cars.car_type,
	   		  cars.car_price
	   		FROM
	   		  car_orders
	   		  LEFT JOIN cars ON car_orders.cid = cars.id
	         WHERE car_orders.id=1
	*/
	id := c.Query("id")
	var list models.CarOrderList
	redis_key := "CarOrderDetail_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("car_orders").
			Select(
				"car_orders.id",
				"car_orders.car_order_code",
				"car_orders.car_order_name",
				"car_orders.car_order_price",
				"cars.car_code",
				"cars.car_type",
				"cars.car_price",
			).
			Joins("LEFT JOIN cars ON car_orders.cid = cars.id").
			Find(&list)

		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
		"msg":  "ok",
	})
	return

}
